package com.authority.controller;

import com.authority.OpServiceException;
import com.authority.entry.AuthMenuActionDO;
import com.authority.entry.AuthMenuDO;
import com.authority.query.AuthMenuQuery;
import com.authority.service.AuthMenuService;
import com.authority.vo.AuthMenuVO;
import com.suixun.util.Constant;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 菜单管理
 * @author limeng.g
 *
 */
@Controller
@RequestMapping(value = "/menuManager")
public class MenuManager extends BaseManager {

	@Resource
	private AuthMenuService authMenuService;

	/**
	 * 新增权限菜单
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/saveMenu.json")
	@ResponseBody
	public Object doSaveMenu(String param) {
		try {
			AuthMenuVO db = checkParam(param, AuthMenuVO.class);
			db.setSysId(getSysId());
			db.setOperatorId(getUserId());
			authMenuService.saveAuthMenu(db);
			return success();
		} catch (Exception e) {
			if (e instanceof OpServiceException) {
				return fail(e);
			}
			logger("MenuManager doSaveMenu:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 修改权限菜单
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/updateMenu.json")
	@ResponseBody
	public Object doUpdateMenu(String param) {
		try {
			AuthMenuVO db = checkParam(param, AuthMenuVO.class);
			db.setSysId(getSysId());
			db.setOperatorId(getUserId());
			authMenuService.updateAuthMenu(db);
			return success();
		} catch (Exception e) {
			if (e instanceof OpServiceException) {
				return fail(e);
			}
			logger("MenuManager doUpdateMenu:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	/**
	 * 删除权限菜单
	 * @param sysId
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value = "/removeMenu.json")
	@ResponseBody
	public Object doRemoveMenu(String param) {
		
		try {
			AuthMenuVO db = checkParam(param, AuthMenuVO.class);
			authMenuService.deleteAuthMenuByids(db.getIds(),getSysId());
			return success();
		} catch (Exception e) {
			if (e instanceof OpServiceException) {
				return fail(e);
			}
			logger("MenuManager removeMenu:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 分页查询权限菜单列表
	 * @param sysId
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value = "/getMenuPage.json")
	@ResponseBody
	public Object getMenuPage(String param) {
		try {
			return success(authMenuService.listAuthMenu(checkParam(param, AuthMenuQuery.class)));
		} catch (Exception e) {
			if (e instanceof OpServiceException) {
				return fail(e);
			}
			logger("MenuManager getMenuPage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	
	
	
	/**
	 * 获取菜单树
	 * 
	 * @param sysId
	 * @return
	 */
	@RequestMapping(value = "/treeMenu.json")
	@ResponseBody
	public Object doTreeMenu(Long sysId) {
		try {
			checkParams(sysId);
			
			return success(authMenuService.treeAuthMenuBySysId(sysId));
		} catch (Exception e) {
			return fail(e);
		}
	}

	/**
	 * 获取菜单信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getMenuById.json")
	@ResponseBody
	public Object doGetMenuById(Long id) {

		try {
			checkParams(id);
			
			return success(authMenuService.getAuthMenuDetailById(id));
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e);
		}
	}

	/**
	 * 更新菜单 
	 * @param sysId  所属系统
	 * @param id 菜单id
	 * @param name  名称
	 * @param parentId  父菜单id
	 * @param code  编码
	 * @param num 顺序号
	 * @param status 状态
	 * @param url 链接
	 * @param isLeaf 是否叶子 
	 * @param actions  功能列表
	 * @param deletedActions 将要删除的功能列表
	 * @return
	 */
	/*@RequestMapping(value = "/updateMenu.json")
	@ResponseBody
	public Object doUpdateMenu(Long sysId,Long id,String name, Long parentId,String code, Integer num,Integer status,
			String url, Integer isLeaf, String actions, String deletedActions) {
		
		try {
			checkParams(sysId,id,name,parentId,code,num,status,isLeaf);
			
			AuthMenuDO db = authMenuService.getAuthMenuById(id);
			db.setName(name);
			db.setParentId(parentId);
			db.setCode(code);
			db.setNum(num);
			db.setUrl(url);
			db.setStatus(status);
			db.setSysId(sysId);
			db.setIsLeaf(isLeaf);
			db.setOperatorId(getUserId());
			
			authMenuService.updateAuthMenu(db, getUserId(),actions,deletedActions);
			
			return success();
		} catch (Exception e) {
			return fail(e);
		}
	}*/
	
	/**
	 * 添加菜单
	* @param sysId  所属系统
	 * @param name  名称
	 * @param parentId  父菜单id
	 * @param code  编码
	 * @param num 顺序号
	 * @param status 状态
	 * @param url 链接
	 * @param isLeaf 是否叶子 
	 * @param actions  功能列表
	 * @return
	 */
	/*@RequestMapping(value = "/saveMenu.json")
	@ResponseBody
	public Object doSaveMenu( Long sysId, String name,Long parentId, String code, Integer num,Integer status,
			String url,Integer isLeaf, String actions) {
		
		try {
			
			checkParams(sysId,name,parentId,code,num,status,isLeaf);
			
			AuthMenuDO db = new AuthMenuDO();
			db.setName(name);
			db.setParentId(parentId);
			db.setCode(code);
			db.setNum(num);
			db.setUrl(url);
			db.setStatus(status);
			db.setSysId(sysId);
			db.setIsLeaf(isLeaf);
			db.setOperatorId(getUserId());
			
			List<AuthMenuActionDO> list = Lists.newArrayList();
			if(StringUtils.isNotEmpty(actions)){
				for(String s : actions.split(",")){
					AuthMenuActionDO adb = new AuthMenuActionDO();
					adb.setActionId(NumberUtils.toLong(s));
					adb.setOperatorId(getUserId());
					adb.setSysId(sysId);
					list.add(adb);
				}
			}
			
			authMenuService.saveAuthMenu(db, list);
			
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return fail(e);
		}
	}*/
	/*@RequestMapping(value = "/removeMenu.json")
	@ResponseBody
	public Object doRemoveMenu( Long sysId, Long menuId) {
		
		try {
			checkParams(sysId, menuId);

			authMenuService.removeAuthMenu(menuId, sysId);
			return success();
		} catch (Exception e) {
			return fail(e);
		}
	}*/
	
	@RequestMapping(value = "/removeMenuTree.json")
	@ResponseBody
	public Object doRemoveMenuTree( Long sysId,Long menuId) {
		
		try {
			checkParams(sysId, menuId);

			authMenuService.removeAuthMenuTree(menuId, sysId);
			return success();
		} catch (Exception e) {
			return fail(e);
		}
	}
}
